<!--
 * @Author: jingjingzheng 1742420988@qq.com
 * @Date: 2023-12-17 14:18:07
 * @LastEditors: jingjingzheng 1742420988@qq.com
 * @LastEditTime: 2023-12-31 03:41:11
 * @FilePath: \mdbj-ui\src\views\datamanager\roadsurface\dataPanel.vue
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
-->
<template>
  <div>
    <div style="display: flex; justify-content: flex-end">
      <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
          <el-button
            type="primary"
            plain
            icon="el-icon-plus"
            size="mini"
            v-if="localname == 'editPanel'"
            :disabled="isroaddie"
            @click="handleAddroadsurface"
            v-hasPermi="['datamanager:roadsurface:add']"
            >新增</el-button
          >
        </el-col>
        </el-col>
    </el-row>
  </div>
  <el-row>
    <!-- 显示查询结果表单 -->
    <el-table
      :data="roadsurfacefeatures"
      v-loading="loading"
      stripes
      highlight-current-row
      :height="tableHeight"
    >
      <el-table-column label="序号" align="center" type="index" width="50"> </el-table-column>
      <el-table-column label="道路名称" align="center" :show-overflow-tooltip="true">{{roadname}}</el-table-column>
      <el-table-column label="保洁公司" align="center" :show-overflow-tooltip="true">{{roadcleancompany}}</el-table-column>
      <!-- <el-table-column
      label="保洁公司"
      width="250"
      align="center"
      :show-overflow-tooltip="true"
      prop="attributes.cleancompany"
      ></el-table-column> -->
      <el-table-column
        label="道路功能"
        width="100"
        align="center"
        :show-overflow-tooltip="true"
        prop="attributes.roadfunction"
      >
        <template slot-scope="scope">
          <dict-tag
          :options="dict.type.sys_roadfunction"
          :value="scope.row.attributes.roadfunction"
          />
        </template>
      </el-table-column>
      <el-table-column
        label="面积(㎡)"
        width="100"
        align="center"
        :show-overflow-tooltip="true"
      >
        <template slot-scope="scope">
          <div v-if="scope.row.attributes.occupiedarea">
            {{ scope.row.attributes.occupiedarea.toFixed(2) }}
          </div>
          <div v-if="!scope.row.attributes.occupiedarea">
            {{ scope.row.attributes.occupiedarea}}
          </div>
        </template>
      </el-table-column>
      <el-table-column
        label="状态"
        width="100"
        align="center"
        :show-overflow-tooltip="true"
        prop="attributes.data_status"
      >
        <template slot-scope="scope">
          <dict-tag
          :options="dict.type.road_data_status"
          :value="scope.row.attributes.data_status"
          />
        </template>
      </el-table-column>

      <!-- 操作模块 -->
      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
      >
        <template slot-scope="scope">
          <el-button
            v-if="localname == 'editPanel' && scope.row.attributes.data_status =='1'"
            :disabled="isroaddie"
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['datamanager:roadsurface:edit']"
          >修改</el-button>
          <el-button
          size="mini"
          type="text"
          icon="el-icon-location-outline"
          @click="handleLocate(scope.row, scope.index)"
          >定位</el-button
          >
          <!-- 判断数据状态显示删除按钮 -->
          <el-button
            v-if="localname == 'editPanel' && scope.row.attributes.data_status =='1'"
            :disabled="isroaddie"
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['datamanager:roadsurface:remove']"
          >删除</el-button>

        </template>
      </el-table-column>
    </el-table>
  </el-row>
<el-row>
  <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      layout="total, sizes,prev, pager, next"
      @pagination="getList"
    />
  </el-row>
  </div>
</template>

<script>
import FeatureLayer from "@arcgis/core/layers/FeatureLayer";
import axios from "axios";

export default {
  props: {
    mapview: { default: () => {} },
    feature: { default: () => {} },
    roadcode: {
      type: String,
    },
    roadname: {
      type: String,
    },
    roadcleancompany: {
      type: String,
    },
    localname: {
      type: String,
    },
    isedit: {
      type: Boolean,
    },
    isroaddie: {
      type: Boolean,
    },
  },
  components: {
    FeatureLayer,
  },
  inject: ["toggleComponent"],
  dicts: [
    "sys_roadclass_js",
    "sys_cleancompany",
    "sys_roadfunction",
    "sys_sfsidewalk",
    "road_data_status"
    ],
  computed: {
    theme() {
      return this.$store.state.settings.theme;
    },
  },
  name: 'MdbjUiDatalPanel',

  data() {
    return {
      queryParams: {
        pageNum: 1,
        pageSize: 20,
      },
      // 总条数
      total: 0,
      // 遮罩层
      loading: false,
      // 道路面要素图层url
      roadsurfacefeatureLayerUrl:
        window.mapConfig.operationLayers.featurelayers["roadsurfacelayer"].url,
      // 道路面图层视图
      roadsurfacelayerView: undefined,
      // 道路面要素图层
      roadsurfacefeatureLayer: undefined,
      // 道路面要素集合
      roadsurfacefeatures: [],
      // 道路面高亮
      roadsurfacehighlight: undefined,
      // 表格高度
      tableHeight: "60vh",
      // 选中的要素
      selectFeatures: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 选中要素数量
      selectnum: 0,
    }
  },
  activated() {
    console.log("roadsurfaceDataPanel activated");
    this.initFeatureLayer();
    let _self = this;
  },
  mounted() {
    console.log("roadsurfaceDataPanel mounted");
    this.initFeatureLayer();
    let _self = this;
    // console.log("total:",_self.total);
    // console.log("code:",_self.roadcode);
  },
  deactivated(){
    let _self = this;
    console.log("deactivated");
    _self.roadsurfacehighlight?.remove();
    _self.mapview.map.layers.remove(_self.roadsurfacefeatureLayer);
  },
  methods: {
    /**
     * @description: 初始化roadsurfacefeatureLayer
     * @return {*}
     */
    initFeatureLayer() {
      let _self = this;
      _self.roadsurfacefeatureLayer = new FeatureLayer({
        title: "道路面",
        url: this.roadsurfacefeatureLayerUrl,
        outFields: ["objectid"],
        definitionExpression: `code='${_self.roadcode}'`,
      });
      console.log(
        "definitionExpression",
        _self.roadsurfacefeatureLayer.definitionExpression
      );
      _self.mapview.map.add(_self.roadsurfacefeatureLayer,1);
      // 获取当前图层视图
      _self.mapview.whenLayerView(_self.roadsurfacefeatureLayer).then((layerView) => {
        // console.log("roadsurface layerView loaded");
        _self.roadsurfacelayerView = layerView;
        this.getList();
      });
    },
    /**
     * @description: 调用arcgis js api查询数据
     * @return {*}
    */
    getList() {
      let _self = this;
      _self.loading = true;
      // 先构建用于查询数据总条数的查询参数对象
      let queryParams = {
        // 输出字段
        outFields: ["objectid"],
        where: `code='${_self.roadcode}'`,
      };
      // 首先查询数据总条数用于渲染分页组件值
      _self.roadsurfacefeatureLayer.queryFeatureCount(queryParams).then((numResults) => {
        _self.total = numResults;
      });
      // 分页查询并在表格展示数据
      queryParams = {
        start: (_self.queryParams.pageNum - 1) * _self.queryParams.pageSize,
        num: _self.queryParams.pageSize,
        outFields: ["*"],
        returnGeometry: true,
        orderByFields: ["objectid"],
        where: `code='${_self.roadcode}'`,
      };
      // 查询数据
      _self.roadsurfacefeatureLayer
        .queryFeatures(queryParams)
        .then((featureSet) => {
          _self.roadsurfacefeatures = featureSet.features;
          _self.loading = false;
        });
    },
    /**
     * @description: 定位按钮操作
     * @param {*} row
     * @return {*}
     */
    async handleLocate(row) {
      let _self = this;
      let feature = undefined;
      feature = row;
      // 移除前一个高亮图形
      _self.roadsurfacehighlight?.remove();
      // console.log(feature);
      // 高亮显示当前页要素;
      _self.roadsurfacehighlight = _self.roadsurfacelayerView.highlight(feature);
      if (feature.geometry == null) {
        _self.$message.error("暂无空间信息");
        return;
      }
      _self.mapview
        .goTo({
          target: feature.geometry,
          // zoom: _self.zoomLevel,
        })
        .catch((error) => {
          console.error(error);
        });
    },
    /**
     * @description: 多选框选中数据
     * @param {*} selection 选中的行
     * @return {*}
     */
    handleSelectionChange(selection) {
      // this.ids = selection.map((item) => item.attributes.objectid);
      this.selectFeatures = selection;
      this.single = selection.length != 1;
      this.multiple = !selection.length;
    },
    handleAddroadsurface() {
      let _self = this;
      const paramObj = {
        feature: _self.feature,
        roadsurfacelayerView:_self.roadsurfacelayerView,
        roadsurfacefeatureLayer: _self.roadsurfacefeatureLayer,
        localname:_self.localname,
      };
      this.toggleComponent("roadsurfaceAddPanel", paramObj);
    },
    /**
     * @description: 删除按钮
     * @return {*}
     */
    handleDelete(row) {
      let _self = this;
      _self.selectnum = _self.selectFeatures.length
      this.$confirm("此操作将永久删除该数据, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          const deleteFeatures =
            row == null && _self.selectFeatures.length != 0
              ? _self.selectFeatures
              : [row];
          const edits = {
            deleteFeatures: deleteFeatures,
          };
          // 删除要素
          _self.roadsurfacefeatureLayer.applyEdits(edits).then((editsResult) => {
            this.$message({
              type: "success",
              message: "删除成功!",
            });
            console.log(editsResult);
            _self.getList();
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
      },
    /**
     * @description: 修改按钮
     * @return {*}
     */
    async handleUpdate(row) {
      let _self = this;
      _self.handleLocate(row);
      let paramObj = {
        feature: _self.feature,
        roadsurfacefeature: row,
        roadsurfacelayerView:_self.roadsurfacelayerView,
        roadsurfacefeatureLayer: _self.roadsurfacefeatureLayer,
        localname:_self.localname,
      };
      _self.toggleComponent("roadsurfaceEditPanel", paramObj);
    },
    removeRoadSurfaceLayer() {
      let _self = this;
      _self.mapview.map.layers.remove(_self.roadsurfacefeatureLayer);
    },
  },
};
</script>

<style lang="less" scoped>

</style>
